package com.todoit.wavebot.server;

import com.google.wave.api.*;

@SuppressWarnings("serial")
public class WaveResponder extends AbstractRobotServlet {

	@Override
	public void processEvents(RobotMessageBundle bundle) {
		Wavelet wavelet = bundle.getWavelet();
        
		
	    if (bundle.wasSelfAdded()) {
	      Blip blip = wavelet.appendBlip();
	      TextView textView = blip.getDocument();
	      textView.append("Hello, this is TodoIt wavebot.");
	      textView.append("Wavelet creator: " + wavelet.getCreator());
	      // iterate over participants, scan for their IDs in the database.
	      for (String Participant : wavelet.getParticipants())
	      {
	    	  // if participant is in database. FIXME: bad comparison?
	    	  if (Participant=="coutal@googlewave.com")
	    		  textView.append("Hello " + Participant +
	    		  ", monitoring conversation for possible tasks for you.");
	    	  else
	    		  textView.append("Hello " + Participant + ", you do not have a task wave set." +
	    				  "Please invite me to your task list so I can suggest tasks for you." +
	    				  "You may wish to read my documentation at http://todoit-wavebot.appengine.google.com");
	      }
	      
	    }
	    
	    if (bundle.wasSelfRemoved()) { // hoping this gets called when wave is deleted
		      // check DB if it was our master task wavelet
	    	  // if so, remove us from the DB and set us to uninitialized again
	    	  // otherwise, nothing to do in here.
		    }

	    // this procedure needs to discern between updates to the task gadget
	    // and between regular conversation updates
	    for (Event e: bundle.getEvents()) {
	      Blip blip = wavelet.appendBlip();
  	      TextView textView = blip.getDocument();
	      if (e.getType() == EventType.WAVELET_PARTICIPANTS_CHANGED) {
	    	  for (String Participant: e.getAddedParticipants())
	    	  {
	    		  // do participant scan (should this be a util?)
	    		  // make due note to ignore selfbot, this will trigger too...
	    		  
	    	      textView.append("Todoit responder version: N/A");
	    		  if (Participant=="coutal@googlewave.com")
		    		  textView.append("Hello coutal, monitoring conversation for possible tasks for you.");
	    	  }
	      }
	      else
	      {
	    	  textView.append("Scanning for added items");
	    	  for (String test : wavelet.getDataDocuments().values())
		    	  textView.append("Item:" + test);
	      }
	    	  // do I need to scan for removed participants? probably not
	    	  // because i'm not storing state about list participants yet.
	      }
	    }

		
	}

